package cn.community.care.module.system.service.articlelike;

import cn.community.care.module.system.controller.admin.articlelike.vo.ArticleLikePageReqVO;
import cn.community.care.module.system.controller.admin.articlelike.vo.ArticleLikeSaveReqVO;
import cn.community.care.module.system.dal.dataobject.articlelike.ArticleLikeDO;
import cn.community.care.module.system.dal.mysql.articlelike.ArticleLikeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 文章点赞 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ArticleLikeServiceImpl implements ArticleLikeService {

    @Resource
    private ArticleLikeMapper articleLikeMapper;

    @Override
    public Long createArticleLike(ArticleLikeSaveReqVO createReqVO) {
        // 插入
        ArticleLikeDO articleLike = BeanUtils.toBean(createReqVO, ArticleLikeDO.class);
        articleLikeMapper.insert(articleLike);
        // 返回
        return articleLike.getId();
    }

    @Override
    public void updateArticleLike(ArticleLikeSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleLikeExists(updateReqVO.getId());
        // 更新
        ArticleLikeDO updateObj = BeanUtils.toBean(updateReqVO, ArticleLikeDO.class);
        articleLikeMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticleLike(Long id) {
        // 校验存在
        validateArticleLikeExists(id);
        // 删除
        articleLikeMapper.deleteById(id);
    }

    private void validateArticleLikeExists(Long id) {
        if (articleLikeMapper.selectById(id) == null) {
//            throw exception(ARTICLE_LIKE_NOT_EXISTS);
        }
    }

    @Override
    public ArticleLikeDO getArticleLike(Long id) {
        return articleLikeMapper.selectById(id);
    }

    @Override
    public PageResult<ArticleLikeDO> getArticleLikePage(ArticleLikePageReqVO pageReqVO) {
        return articleLikeMapper.selectPage(pageReqVO);
    }

}